package com.carbon.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carbon.common.core.domain.AjaxResult;
import com.carbon.common.core.text.Convert;
import com.carbon.domain.Carrier;
import com.carbon.domain.Contract;
import com.carbon.service.ContractService;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import com.carbon.service.ContractService;
import com.carbon.vo.SearchParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 销售合同审核(Contract)表控制层
 *
 * @author makejava
 * @since 2023-09-14 10:35:43
 */
@RestController
@Slf4j
@RequestMapping("/task/contract")
public class ContractController {
    /**
     * 服务对象
     */
    @Resource
    private ContractService contractService;
    @GetMapping("/list")
    public AjaxResult list(SearchParams params) {
        Page<Contract> page = new Page<>(params.getPageNum(), params.getPageSize());
        LambdaUpdateWrapper<Contract> lqw = new LambdaUpdateWrapper<Contract>()
                .eq(ObjectUtil.isNotEmpty(params.getCreateStatus()), Contract::getCreateStatus, params.getCreateStatus())
                .eq(ObjectUtil.isNotEmpty(params.getUpdateStatus()), Contract::getUpdateStatus, params.getUpdateStatus());

        String search = params.getSearch();
        if (ObjectUtil.isNotEmpty(search)) {
            lqw.like(Contract::getOrderId, search).or()
                    .like(Contract::getCreateBy, search).or()
                    .like(Contract::getUpdateBy, search).or()
                    .like(Contract::getCustomerName, search).or();
        }
        String dateTime = params.getDateTime();
        if (ObjectUtil.isNotEmpty(dateTime)) {
            lqw.like(Contract::getCreateTime, dateTime).or()
                    .like(Contract::getUpdateTime, dateTime)
                    .like(Contract::getDeliveryTime, dateTime)
                    .like(Contract::getSignDate, dateTime);
        }
        page = contractService.page(page, lqw);
        List<Contract> records = page.getRecords();
        if (ObjectUtil.isEmpty(records)) {
            return AjaxResult.success();
        }
        return AjaxResult.success()
                .put("rows", records)
                .put("total", page.getTotal());
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
         Contract contract = contractService.getById(id);
        return AjaxResult.success(contract);
    }

    /**
     * 新增数据
     *
     * @param contract 实体对象
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult insert(@RequestBody Contract contract) {
        contractService.save(contract);
        return AjaxResult.success();
    }

    /**
     * 修改数据
     *
     * @param contract 实体对象
     * @return 修改结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody Contract contract) {
        contractService.updateById(contract);
        return AjaxResult.success();
    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Long id) {
        String s = Convert.toStr(id);
        List<String> split = StrUtil.split(s, ",");
        LambdaUpdateWrapper<Contract> lqw = new LambdaUpdateWrapper<Contract>()
                .in(Contract::getId, split);
        return AjaxResult.success(contractService.remove(lqw));
    }
    /**
     * 修改审核状态
     * @param id
     * @param updateStatus
     * @param comments
     * @return
     */
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(int id, String updateStatus,String comments) {
        Contract contract=new Contract();
        contract.setId(id);
        contract.setUpdateStatus(updateStatus);
        contract.setComments(comments);
        // 审核通过
        if ("1".equalsIgnoreCase(updateStatus)) {
            contract.setUpdateStatus("1");
            contract.setCreateStatus("1");
        } else {
            contract.setUpdateStatus("0");
            contract.setCreateStatus("1");
        }
        return contractService.updateById(contract) ? AjaxResult.success() : AjaxResult.error();
    }
}


